/*
 * TypyStartovniKategorieDialog.java
 *
 * Created on 2. leden 2007, 9:29
 */

package cz.results.winter6.view.dialogs.stkategorie;

import cz.results.winter6.model.KategorieConfigList;
import java.util.TreeMap;
import javax.swing.JDialog;

public class TypyStartovniKategorieDialog extends JDialog {
    
    private TypyStartovniKategorieTableModel tm;
    private TreeMap<String,KategorieConfigList> startovniKategorieConfigMap;
    
    public TypyStartovniKategorieDialog(JDialog parent, boolean modal, TreeMap<String,KategorieConfigList> startovniKategorieConfigMap ) {
        super(parent, modal);
        
        setStartovniKategorieConfigMap(startovniKategorieConfigMap);
        setTm(new TypyStartovniKategorieTableModel(startovniKategorieConfigMap));
        
        initComponents();
    }
    
    public TypyStartovniKategorieTableModel getTm() {
        return tm;
    }
    
    public void setTm(TypyStartovniKategorieTableModel tm) {
        this.tm = tm;
    }
    
    public TreeMap<String, KategorieConfigList> getStartovniKategorieConfigMap() {
        return startovniKategorieConfigMap;
    }
    
    public void setStartovniKategorieConfigMap(TreeMap<String, KategorieConfigList> startovniKategorieConfigMap) {
        this.startovniKategorieConfigMap = startovniKategorieConfigMap;
    }
    
        
    public void upravTypKategorie(){
        final JDialog parent = this;
        
        int selectedRow = jTable1.getSelectedRow();
        
        if(selectedRow!=-1) {                            //musi byt vybran nejaky radek
            
            final String nazevTypuKategorie = getTm().getKey(selectedRow);
            
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new TypStartovniKategorieDialog(parent, true, nazevTypuKategorie, getStartovniKategorieConfigMap(), false).setVisible(true);
                    getTm().aktualizujKeyList();
                    getTm().fireTableDataChanged();
                }
            });
        }
    }
    
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButtonNovy = new javax.swing.JButton();
        jButtonUprav = new javax.swing.JButton();
        jButtonSmaz = new javax.swing.JButton();
        jButtonZrus = new javax.swing.JButton();

        setTitle("konfigurace typu startovn\u00edch kategori\u00ed");
        setLocationByPlatform(true);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });

        jTable1.setModel(tm);
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });

        jScrollPane1.setViewportView(jTable1);

        jButtonNovy.setIcon(new javax.swing.ImageIcon(getClass().getResource("/cz/results/winter6/view/icons/edit_add.png")));
        jButtonNovy.setText("nov\u00fd");
        jButtonNovy.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonNovyActionPerformed(evt);
            }
        });

        jButtonUprav.setIcon(new javax.swing.ImageIcon(getClass().getResource("/cz/results/winter6/view/icons/edit.png")));
        jButtonUprav.setText("uprav");
        jButtonUprav.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonUpravActionPerformed(evt);
            }
        });

        jButtonSmaz.setIcon(new javax.swing.ImageIcon(getClass().getResource("/cz/results/winter6/view/icons/edit_remove.png")));
        jButtonSmaz.setText("sma\u017e");
        jButtonSmaz.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSmazActionPerformed(evt);
            }
        });

        jButtonZrus.setIcon(new javax.swing.ImageIcon(getClass().getResource("/cz/results/winter6/view/icons/button_cancel.png")));
        jButtonZrus.setText("zru\u0161");
        jButtonZrus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonZrusActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 594, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButtonNovy)
                        .addGap(18, 18, 18)
                        .addComponent(jButtonUprav)
                        .addGap(15, 15, 15)
                        .addComponent(jButtonSmaz)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 196, Short.MAX_VALUE)
                        .addComponent(jButtonZrus)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jButtonNovy)
                        .addComponent(jButtonUprav)
                        .addComponent(jButtonSmaz))
                    .addComponent(jButtonZrus))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        if(evt.getClickCount()==2){
            upravTypKategorie();
        }
    }//GEN-LAST:event_jTable1MouseClicked
    
    private void jButtonZrusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonZrusActionPerformed
        setVisible(false);
        dispose();
    }//GEN-LAST:event_jButtonZrusActionPerformed
    
    private void jButtonSmazActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSmazActionPerformed
        int selectedRow = jTable1.getSelectedRow();
        
        if(selectedRow!=-1) {                            //musi byt vybran nejaky radek
            getStartovniKategorieConfigMap().remove(getTm().getKey(selectedRow));
            getTm().aktualizujKeyList();
            getTm().fireTableDataChanged();
        }
    }//GEN-LAST:event_jButtonSmazActionPerformed
    
    private void jButtonUpravActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonUpravActionPerformed
        upravTypKategorie();
    }//GEN-LAST:event_jButtonUpravActionPerformed
    
    private void jButtonNovyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonNovyActionPerformed
        final JDialog parent = this;
        
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TypStartovniKategorieDialog(parent, true, "", getStartovniKategorieConfigMap(), true).setVisible(true);
                getTm().aktualizujKeyList();
                getTm().fireTableDataChanged();
            }
        });
    }//GEN-LAST:event_jButtonNovyActionPerformed
    
    /** Closes the dialog */
    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
        setVisible(false);
        dispose();
    }//GEN-LAST:event_closeDialog
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonNovy;
    private javax.swing.JButton jButtonSmaz;
    private javax.swing.JButton jButtonUprav;
    private javax.swing.JButton jButtonZrus;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables
    
}
